package com.spark.client.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

/**
 * 星火大模型响应类
 */
public class SparkResponse {
    
    /**
     * 错误码
     */
    private Integer code;
    
    /**
     * 错误信息
     */
    private String message;
    
    /**
     * 请求唯一ID
     */
    private String sid;
    
    /**
     * 响应ID
     */
    private String id;
    
    /**
     * 创建时间
     */
    private Long created;
    
    /**
     * 选择列表
     */
    private List<Choice> choices;
    
    /**
     * 使用情况
     */
    private Usage usage;
    
    /**
     * 错误信息（OpenAI 格式）
     */
    private ErrorInfo error;

    /**
     * 选择项
     */
    public static class Choice {
        private SparkMessage message;
        private Delta delta;
        private Integer index;
        @JsonProperty("finish_reason")
        private String finishReason;

        // Getters and Setters
        public SparkMessage getMessage() {
            return message;
        }

        public void setMessage(SparkMessage message) {
            this.message = message;
        }

        public Delta getDelta() {
            return delta;
        }

        public void setDelta(Delta delta) {
            this.delta = delta;
        }

        public Integer getIndex() {
            return index;
        }

        public void setIndex(Integer index) {
            this.index = index;
        }

        public String getFinishReason() {
            return finishReason;
        }

        public void setFinishReason(String finishReason) {
            this.finishReason = finishReason;
        }
    }

    /**
     * 流式响应增量
     */
    public static class Delta {
        private String role;
        private String content;
        @JsonProperty("tool_calls")
        private List<ToolCall> toolCalls;

        // Getters and Setters
        public String getRole() {
            return role;
        }

        public void setRole(String role) {
            this.role = role;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public List<ToolCall> getToolCalls() {
            return toolCalls;
        }

        public void setToolCalls(List<ToolCall> toolCalls) {
            this.toolCalls = toolCalls;
        }
    }

    /**
     * 工具调用
     */
    public static class ToolCall {
        private String id;
        private String type;
        private Function function;

        // Getters and Setters
        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public Function getFunction() {
            return function;
        }

        public void setFunction(Function function) {
            this.function = function;
        }
    }

    /**
     * 函数调用
     */
    public static class Function {
        private String name;
        private String arguments;

        // Getters and Setters
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getArguments() {
            return arguments;
        }

        public void setArguments(String arguments) {
            this.arguments = arguments;
        }
    }

    /**
     * 使用情况
     */
    public static class Usage {
        @JsonProperty("prompt_tokens")
        private Integer promptTokens;
        @JsonProperty("completion_tokens")
        private Integer completionTokens;
        @JsonProperty("total_tokens")
        private Integer totalTokens;

        // Getters and Setters
        public Integer getPromptTokens() {
            return promptTokens;
        }

        public void setPromptTokens(Integer promptTokens) {
            this.promptTokens = promptTokens;
        }

        public Integer getCompletionTokens() {
            return completionTokens;
        }

        public void setCompletionTokens(Integer completionTokens) {
            this.completionTokens = completionTokens;
        }

        public Integer getTotalTokens() {
            return totalTokens;
        }

        public void setTotalTokens(Integer totalTokens) {
            this.totalTokens = totalTokens;
        }
    }

    /**
     * 错误信息
     */
    public static class ErrorInfo {
        private String message;
        private String type;
        private String param;
        private String code;

        // Getters and Setters
        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getParam() {
            return param;
        }

        public void setParam(String param) {
            this.param = param;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }
    }

    // Main class Getters and Setters
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Long getCreated() {
        return created;
    }

    public void setCreated(Long created) {
        this.created = created;
    }

    public List<Choice> getChoices() {
        return choices;
    }

    public void setChoices(List<Choice> choices) {
        this.choices = choices;
    }

    public Usage getUsage() {
        return usage;
    }

    public void setUsage(Usage usage) {
        this.usage = usage;
    }

    public ErrorInfo getError() {
        return error;
    }

    public void setError(ErrorInfo error) {
        this.error = error;
    }

    // 便捷方法

    /**
     * 获取第一个选择的内容（简化方法）
     * @return 内容字符串，如果没有内容则返回 null
     */
    public String getContent() {
        if (choices != null && !choices.isEmpty()) {
            Choice firstChoice = choices.get(0);
            if (firstChoice.getMessage() != null) {
                return firstChoice.getMessage().getContent();
            }
        }
        return null;
    }

    /**
     * 获取流式响应的增量内容（简化方法）
     * @return 增量内容字符串，如果没有内容则返回 null
     */
    public String getDeltaContent() {
        if (choices != null && !choices.isEmpty()) {
            Choice firstChoice = choices.get(0);
            if (firstChoice.getDelta() != null) {
                return firstChoice.getDelta().getContent();
            }
        }
        return null;
    }

    /**
     * 检查响应是否成功
     * @return true 如果成功，false 如果失败
     */
    public boolean isSuccess() {
        return code != null && code == 0;
    }

    /**
     * 检查是否有内容
     * @return true 如果有内容，false 如果没有内容
     */
    public boolean hasContent() {
        return getContent() != null && !getContent().trim().isEmpty();
    }

    /**
     * 检查是否有增量内容（流式响应）
     * @return true 如果有增量内容，false 如果没有增量内容
     */
    public boolean hasDeltaContent() {
        return getDeltaContent() != null && !getDeltaContent().trim().isEmpty();
    }

    /**
     * 获取错误信息（如果有）
     * @return 错误信息字符串
     */
    public String getErrorMessage() {
        if (error != null && error.getMessage() != null) {
            return error.getMessage();
        }
        return message;
    }
}
